package com.anmingle.pswb.service;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.stp.StpUtil;
import com.anmingle.pswb.entity.LoginUser;

/**
 * @author 安明乐
 * @Title Loginservice
 * @Package IntelliJ IDEA
 * @date 2023/2/20 14:45
 */
public class LoginService {

    public static final String LOGIN_USER_KEY = "Login";

    /**
     * 登录
     * 设置多个
     * @param loginUser 登录用户信息
     */
    public static void login(LoginUser loginUser) {
        SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
        StpUtil.login(loginUser.getId());
        setLoginUser(loginUser);
    }

    /**
     * 写入token session
     *
     */
    public static void setLoginUser(LoginUser loginUser) {
        StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
    }


    /**
     * 获取用户信息
     */
    public static LoginUser getLoginUser() {
        LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
        if (loginUser != null) {
            return loginUser;
        }
        loginUser = (LoginUser) StpUtil.getTokenSession().get(LOGIN_USER_KEY);
        SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
        return loginUser;
    }

}
